package com.haorui.maildemo3;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private Button send;
    private EditText userid;
    private EditText password;
    private EditText from;
    private EditText to;
    private EditText subject;
    private EditText body;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        send = (Button) findViewById(R.id.send);
        userid = (EditText) findViewById(R.id.userid);
        password = (EditText) findViewById(R.id.password);
        from = (EditText) findViewById(R.id.from);
        to = (EditText) findViewById(R.id.to);
        subject = (EditText) findViewById(R.id.subject);
        body = (EditText) findViewById(R.id.body);
        send.setText("Send Mail");
        userid.setText("xxx@163.com"); //你的邮箱用户名
        password.setText("xxx");         //你的邮箱登陆密码(有的邮箱为授权码// )
        from.setText("xxx@163.com");//发送的邮箱
        to.setText("xxx@qq.com"); //发到哪个邮件去
        subject.setText("一步到位验证码");//邮件主题
        body.setText("尊敬的用户：您的验证码为6666666");//邮件内容


        send.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                try
                {
                    final MailSenderInfo mailInfo = new MailSenderInfo();
                    mailInfo.setMailServerHost("smtp.163.com");
                    mailInfo.setMailServerPort("25");
                    mailInfo.setValidate(true);
                    mailInfo.setUserName(userid.getText().toString());  //你的邮箱地址
                    mailInfo.setPassword(password.getText().toString());//您的邮箱密码
                    mailInfo.setFromAddress(from.getText().toString());
                    mailInfo.setToAddress(to.getText().toString());
                    mailInfo.setSubject(subject.getText().toString());
                    mailInfo.setContent(body.getText().toString());

                    //这个类主要来发送邮件
                    final SimpleMailSender sms = new SimpleMailSender();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            sms.sendTextMail(mailInfo);//发送文体格式
                        }
                    }).start();

                    //sms.sendHtmlMail(mailInfo);//发送html格式

                }
                catch (Exception e) {
                    Log.e("SendMail", e.getMessage(), e);
                }
            }
        });
    }
}